#!/bin/bash
#
##############################################################################
#                                                                            #
#                     Pi-Star MMDVM HS_HAT Reset Tool                        #
#                                                                            #
#     Version 1.2, Code, Design and Development by Andy Taylor (MW0MWZ).     #
#                                                                            #
#     Make it simple to reset an MMDVM bosard from the CLI on Pi-Star.       #
#                                                                            #
##############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

# Detect Pi4 Hardware
if [ "$(/usr/local/bin/platformDetect.sh | grep "Pi 4 Model" | wc -l)" -ge 1 ]; then
  model="Pi4"
else
  model=""
fi

# Make the "boot" option only work on Pi 4 hardware
if [ "${1}" = "boot" ]; then
    if [ "${model}" != "Pi4" ]; then
        exit 0
    fi
fi

if [ -f '/etc/dstar-radio.mmdvmhost' ]; then
    # Get the modem that the user has selected.
    modemSelected=$(grep 'Hardware=' /etc/dstar-radio.mmdvmhost)
    modemSelected=${modemSelected#*=}

    # Get the user friendly version of the selected Modem.
    modemSelectedHuman=$(grep value=\"${modemSelected}\" /var/www/dashboard/admin/configure.php | head -n 1)
    modemSelectedHuman=${modemSelectedHuman#*\">}
    modemSelectedHuman=${modemSelectedHuman::-9}
fi

# Get the Port the modem is on
modemPort=$(sed -n '/^\[Modem\]/,/^\[/p' /etc/mmdvmhost | grep ^"UARTPort" | awk -F '=' '{print $2}')
if [ -z "${modemPort}" ]; then
  modemPort=$(sed -n '/^\[Modem\]/,/^\[/p' /etc/mmdvmhost | grep ^"Port" | awk -F '=' '{print $2}')
fi
if [ -z "${modemPort}" ] || [ "${modemPort}" != "/dev/ttyAMA0" ]; then
  echo -e "Your Modem is not connected to the GPIO"
  exit 0
fi

# If there are no arguments (like "boot") then stop the service...
if [ -z "${1}" ]; then
  echo -e "Resetting Modem: ${modemSelectedHuman}"
  systemctl stop mmdvmhost
fi

# Reset process for MMDVM HS_HAT Board (20,-21,21:-20,21)
echo 20 > /sys/class/gpio/export
echo 21 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio20/direction
echo out > /sys/class/gpio/gpio21/direction
sleep 0.5
echo 0 > /sys/class/gpio/gpio20/value
echo 0 > /sys/class/gpio/gpio21/value
echo 1 > /sys/class/gpio/gpio21/value
sleep 1
echo 0 > /sys/class/gpio/gpio20/value
echo 1 > /sys/class/gpio/gpio20/value
echo 0 > /sys/class/gpio/gpio20/value
sleep 0.5
echo 20 > /sys/class/gpio/unexport
echo 21 > /sys/class/gpio/unexport
sleep 2
message="Modem reset complete"

# If there are no arguments (like "boot") then start the service...
if [ -z "${1}" ]; then
  systemctl start mmdvmhost
  echo -e ${message}
fi

exit 0
